package aes.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import aes.general.AdminControl;

/**
 * The Class AdminCourseStatsPanel. This panel provides statistical information
 * about spesific course for the admin's reports.
 */
public class AdminCourseStatsPanel extends JPanel{

	/**
	 * 
	 */
	 
		private static final long serialVersionUID = 1L;
		private JPanel cp;
		private JLabel lblTeachersGrades;
		private JSeparator separator;
		private JButton btnPrev;
		private JTextField avgtext;
		private JTextField medtext;
		private DefaultTableModel model;
		private JTable table;
		private JTextField numtext;
		private JComboBox CidBox;
		private JComboBox exambox;
		private Vector<Vector<String>> Coursesvec;
		private ArrayList<String> ExamId;
		private ArrayList<Double> stats;
		private JTextField Cname;
		private JLabel lblCourseId;
		private JLabel lblWxamId;
		
		
		/**
		 * Instantiates a new admin course stats panel.
		 * 
		 * @param contentPane
		 *            the content pane
		 */
		public AdminCourseStatsPanel(JPanel contentPane) {
			this.cp = contentPane;
			initialize();
			
		}
		
		/**
		 * Initialize.
		 */
		private void initialize() {
			setLayout(null);
			lblTeachersGrades = new JLabel("Course Statatistics");
			lblTeachersGrades.setFont(new Font("Tempus Sans ITC", Font.BOLD, 21));
			lblTeachersGrades.setBounds(238, 11, 164, 29);
			add(lblTeachersGrades);
			
			separator = new JSeparator();
			separator.setBounds(10, 43, 620, 2);
			add(separator);
			
			btnPrev = new JButton("Back");
			btnPrev.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					AdminControl.setUserInfo(null);
					CardLayout cl = (CardLayout) (cp.getLayout());
					cl.show(cp, "reports");
				}
			});
			btnPrev.setBounds(189, 374, 107, 23);
			add(btnPrev);
		          
			JLabel lblStudents = new JLabel("Cousre Name");
			lblStudents.setBounds(238, 56, 107, 14);
			add(lblStudents);
			
			JLabel lblA = new JLabel("Average");
			lblA.setFont(new Font("Tahoma", Font.BOLD, 14));
			lblA.setBounds(50, 140, 69, 23);
			add(lblA);
			
			avgtext = new JTextField();
			avgtext.setEditable(false);
			avgtext.setFont(new Font("Tahoma", Font.BOLD, 14));
			avgtext.setBounds(150, 141, 47, 20);
			add(avgtext);
			avgtext.setColumns(10);
			
			JLabel lblMedian = new JLabel("Median");
			lblMedian.setFont(new Font("Tahoma", Font.BOLD, 14));
			lblMedian.setBounds(49, 187, 69, 23);
			add(lblMedian);
			
			medtext = new JTextField();
			medtext.setFont(new Font("Tahoma", Font.BOLD, 14));
			medtext.setEditable(false);
			medtext.setBounds(150, 190, 47, 20);
			add(medtext);
			medtext.setColumns(10);
			
			JLabel lblDistribution = new JLabel("Distribution");
			lblDistribution.setFont(new Font("Tahoma", Font.BOLD, 14));
			lblDistribution.setBounds(49, 241, 86, 23);
			add(lblDistribution);
			
			JPanel Tablepanel = new JPanel();
			Tablepanel.setBounds(49, 275, 470, 76);
			add(Tablepanel);
			
			Tablepanel.setLayout(null);
			
			String col[] = {"1","2","3","4","5","6","7","8","9","10"};
			String data[][] = {{}};
			model = new DefaultTableModel(data,col);
			
			
			table = new JTable(model);
			table.setBackground(new Color(245, 255, 250));
			table.setFont(new Font("Tahoma", Font.PLAIN, 12));
			table.setBounds(10, 11, 434, 36);
			Tablepanel.add(table);
			int j = 0;
			model.insertRow(j,new Object[]{"0-9","10-19","20-29","30-39","40-49","50-59","60-69","70-79"
					,"80-89","90-100"});
			
			JLabel lblNumberOfExams = new JLabel("Number of exams : ");
			lblNumberOfExams.setFont(new Font("Tahoma", Font.BOLD, 14));
			lblNumberOfExams.setBounds(270, 146, 162, 29);
			add(lblNumberOfExams);
			
			numtext = new JTextField();
			numtext.setFont(new Font("Tahoma", Font.BOLD, 14));
			numtext.setEditable(false);
			numtext.setBounds(442, 152, 32, 20);
			add(numtext);
			numtext.setColumns(10);
			
			while((Coursesvec = AdminControl.getUserInfo())== null);
			
			Cname = new JTextField();
			Cname.setEditable(false);
			Cname.setBounds(330, 56, 86, 20);
			add(Cname);
			Cname.setColumns(10);
			
			CidBox = new JComboBox();
			CidBox.setBounds(49, 98, 60, 20);
			add(CidBox);
			CidBox.addItem("-----");
			for(int i =0 ;i<Coursesvec.size();i++)
				CidBox.addItem(Coursesvec.get(i).get(0));
			CidBox.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent arg0) {	
					int index = CidBox.getSelectedIndex(); 
					if(CidBox.getSelectedIndex() == 0){
						Cname.setText(null);
						exambox.removeAllItems();
						exambox.addItem("------");
						
					}
					else
					{
						System.out.println(Coursesvec.get(index-1).get(1));
						Cname.setText(Coursesvec.get(index-1).get(1));
						AdminControl.getCourseExamInfo((String) CidBox.getSelectedItem());
						while((ExamId = AdminControl.getExamID()) == null);
						exambox.removeAllItems();
						exambox.addItem("------");
						if(ExamId.get(0).equals("error"))
						{
							JOptionPane.showMessageDialog(null, "Error,No Exams For This Course", "Error",
									JOptionPane.ERROR_MESSAGE);
						}
						else{
							for(String EId:ExamId)
								exambox.addItem(EId);
						}
						AdminControl.setExamID(null);		
						
					}
				}
			});
			
			exambox = new JComboBox();
			exambox.setBounds(141, 100, 60, 20);
			add(exambox);
			exambox.addItem("------");
			
			lblCourseId = new JLabel("Course Id");
			lblCourseId.setBounds(50, 81, 69, 14);
			add(lblCourseId);
			
			lblWxamId = new JLabel("Exam Id");
			lblWxamId.setBounds(141, 81, 56, 14);
			add(lblWxamId);
			
		
			
			
			exambox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {	
					int index = CidBox.getSelectedIndex();
					if(exambox.getSelectedIndex() == 0){
						avgtext.setText(null);
						medtext.setText(null);
						numtext.setText(null);
						model.insertRow(1,new Object[]{null,null,null,null,null,null,null,null,null,null});
						model.removeRow(2);
					}
					else {
						if((String) exambox.getSelectedItem() != null){
						AdminControl.getTeacerExamStat((String) exambox.getSelectedItem());
						while(AdminControl.getGrades()==null);	
						stats = AdminControl.CulcStats(AdminControl.getGrades()); 
						avgtext.setText(stats.get(0).toString());
						medtext.setText(stats.get(1).toString());
						numtext.setText(stats.get(2).toString());
						int[] dis = AdminControl.getDistribution(AdminControl.getGrades());
						model.insertRow(1,new Object[]{dis[0],dis[1],dis[2],dis[3],dis[4],dis[5],dis[6],dis[7],dis[8],dis[9]});
						model.removeRow(2);
						AdminControl.setGrades(null);
						}
					}
				}
			});	
			
		}
	}


